//
//  HMRetweetView.swift
//  WeiBo
//
//  Created by apple on 16/6/27.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class HMRetweetView: UIView {

    var bottomConstraint:  Constraint?
    
    var retweetViewModel: HMStatusCellViewModel?{
    
        didSet{
            
//            contentLabel.text = retweetViewModel?.retweet_statusString
            
            contentLabel.attributedText = retweetViewModel?.retweetAttributeString
            
            bottomConstraint?.uninstall()
            
            if retweetViewModel?.model?.retweeted_status?.pic_urls?.count > 0 {
                
                // 有配图
                pictrueView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                  self.bottomConstraint =  make.bottom.equalTo(pictrueView.snp_bottom).constraint
                })
                
                
                pictrueView.pic_urls = retweetViewModel?.model?.retweeted_status?.pic_urls
                
            } else{
                // 没有配图
                pictrueView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                  self.bottomConstraint =  make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        }
        
    }
    
    // 第一步 -- 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步 -- 定义一个方法,来实现 视图的添加和布局
    private func setupUI(){
        
        backgroundColor = UIColor.yellowColor()
        
        addSubview(contentLabel)
        addSubview(pictrueView)
        
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            make.bottom.equalTo(pictrueView.snp_top)
        }
        
        pictrueView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
//            make.right.equalTo(self.snp_right)
            
            make.height.equalTo(100)
            make.width.equalTo(100)
            
            
        }
        
        //
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint =  make.bottom.equalTo(pictrueView.snp_bottom).constraint
        }
        
    }
    
    // 第三步: 懒加载控件
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        label.font = UIFont.systemFontOfSize(16)
        
        label.text = "转发微博"
        
        label.numberOfLines = 0
        
        return label
    }()
    
    
    private lazy var pictrueView: HMPictureView = HMPictureView()
    

}
